package com.wsjc.manager.service.routine.region.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.wsjc.manager.dao.area.WjAreaDao;
import com.wsjc.manager.dao.city.WjCityDao;
import com.wsjc.manager.dao.province.WjProvinceDao;
import com.wsjc.manager.entity.WjArea;
import com.wsjc.manager.entity.WjCity;
import com.wsjc.manager.entity.WjProvince;
import com.wsjc.manager.entity.vo.map.RegionVo;
import com.wsjc.manager.service.routine.region.WjRegionService;


@Service("wjRegionService")
public class WjRegionServiceImpl implements WjRegionService {
	
	
	@Autowired
	private WjProvinceDao wjProvinceDaoImpl ;
	
	@Autowired
	private WjCityDao wjCityDaoImpl ;
	
	@Autowired
	private WjAreaDao wjAreaDaoImpl ;
	

	@Override
	public List<RegionVo> queryAllProvinces() {
		List<WjProvince> provinces = wjProvinceDaoImpl.listBy(null) ;
		String jsonP = JSONObject.toJSONString(provinces) ;
		List<RegionVo> regionVos = JSON.parseArray(jsonP, RegionVo.class) ;
		return regionVos;
	}

	@Override
	public List<RegionVo> queryAllAreasByCityId(String provinceId, String cityId) {
		Map<String, Object> paramMap = Maps.newHashMap();
		if(null != cityId){
			paramMap.put("cityId", cityId);
		}
		if(null != provinceId){
			provinceId = provinceId.substring(0, 2);
			paramMap.put("provinceId", provinceId);
		}
		List<WjArea> areas = wjAreaDaoImpl.listBy(paramMap);
		String jsonA = JSONObject.toJSONString(areas) ;
		List<RegionVo> regionVos = JSON.parseArray(jsonA, RegionVo.class) ;
		return regionVos;
	}

	@Override
	public List<RegionVo> queryAllCitysByProvinceId(String provinceId) {
		
		Map<String, Object> paramMap = Maps.newHashMap();
		paramMap.put("provinceId", provinceId);
		
		List<WjCity> cities = wjCityDaoImpl.listBy(paramMap);
		String jsonC = JSONObject.toJSONString(cities) ;
		List<RegionVo> regionVos = JSON.parseArray(jsonC, RegionVo.class) ;
		return regionVos;
		
	}

	/**
	 * @param nameType : (1)baidu,(2)gaode.
	 */
	@Override
	public void updataRegionName(String provinceName, String cityName, String areaName, String nameType) {
		
		switch (nameType) {
			case "baidu":
				if(null != areaName){
					wjAreaDaoImpl.updateBaiDuName(provinceName, areaName);
					return;
				}
				if(null != cityName){
					wjCityDaoImpl.updateBaiDuName(provinceName, cityName);
					return;
				}
				break;
	
			default:
				
				if(null != areaName){
					wjAreaDaoImpl.updateGaoDeName(provinceName, areaName);
					return;
				}
				if(null != cityName){
					wjCityDaoImpl.updateGaoDeName(provinceName, cityName);
					return;
				}
				
				break;
		}
		
		
	}

	
	@Override
	public WjArea queryAreaById(String id) {
		return wjAreaDaoImpl.getById(id);
	}

	@Override
	public WjCity queryCityById(String id) {
		return wjCityDaoImpl.getById(id);
	}

	@Override
	public WjProvince queryProvinceById(String id) {
		return wjProvinceDaoImpl.getById(id);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
